<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2022-09-30
 * Time: 12:52
 */

namespace app\services;


use app\models\ConfigLocation;
use app\models\Configs;
use think\facade\Cache;

class ConfigService extends ServiceBase
{
    public function getLists()
    {
        $data = [];

        $type = ConfigLocation::where('isShow',1)->select();

        foreach ($type as $key => $val) {
            $data[$key] = [
                'locationId' => $val->locationId,
                'locationName' => $val->locationName,
                'locationKey' => $val->locationKey
            ];

            $configs = Configs::where(['configLocation' => $val->locationId])->select();
            if (count($configs)) {
                foreach ($configs as $config) {
                    $data[$key]['children'][] = [
                        'configId' => $config->configId,
                        'configName' => $config->configName,
                        'configKey' => $config->configKey,
                        'configValue' => $config->configValue,
                        'configType' => $config->configType,
                        'configDesc' => $config->configDesc,
                    ];
                }
            }
        }

        return $data;
    }

    /**
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAll()
    {
        Cache::delete('configs');
        $data = json_decode(Cache::get('configs'), 1);

        if (!$data) {
            $data = [];
            $configs = Configs::select();

            foreach ($configs as $config) {
                $data[$config->configKey] = $config->configValue;
            }

            Cache::set('configs', json_encode($data, JSON_UNESCAPED_UNICODE), 86400 * 30);
        }

        return $data;
    }

    /**
     * @param string $key
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDetail($key = '')
    {
        $data = $this->getAll();

        return $data[$key];
    }

    /**
     * @param $params
     * @param $siteId
     * @return bool
     * @throws \Exception
     */
    public function update($params)
    {
        try {
            if (empty($params)) {
                return false;
            }

            foreach ($params as $value) {
                $config = Configs::where(['configKey' => $value['configKey']])->find();

                if ($config instanceof Configs) {
                    if (is_array($value['configValue'])) {
                        $value['configValue'] = json_encode($value['configValue'], JSON_UNESCAPED_UNICODE);
                    }
                    $config->configValue = $value['configValue'];
                    $config->save();
                }
            }

            return true;

        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
}